<template>
  <div class="checkout-address">
    <div class="text">
      <div class="none" v-if="!showAddress">您需要先添加收货地址才可提交订单。</div>
      <template v-else>
        <ul>
          <li>
            <span>收<i />货<i />人：</span>{{ showAddress.receiver }}
          </li>
          <li><span>联系方式：</span>{{ showAddress.contact }}</li>
          <li><span>收货地址：</span>{{ showAddress.fullLocation + showAddress.address }}</li>
        </ul>
        <a href="javascript:;">修改地址</a>
      </template>
    </div>
    <div class="action">
      <XtxButton class="btn">切换地址</XtxButton>
      <XtxButton @click="dialogVisible = true" class="btn">添加地址</XtxButton>
    </div>
  </div>
  <!-- 💥 Vue3 新增的标签：瞬间移动/传送/瞬息移动/空间移动 到 body 后 -->
  <Teleport to="body">
    <XtxDialog title="收货地址管理" v-model="dialogVisible">
      <h1>这里还允许放插槽内容</h1>
      <template #footer>
        <XtxButton @click="dialogVisible = false" type="gray" style="margin-right: 20px">取消</XtxButton>
        <XtxButton type="primary">确认</XtxButton>
      </template>
    </XtxDialog>
  </Teleport>
</template>

<script>
import { ref } from 'vue'
export default {
  name: 'CheckoutAddress',
  props: {
    list: {
      type: Array,
      default: () => []
    }
  },
  setup(props) {
    // 显示的地址：测试账号 sujiehao   密码：123456
    const showAddress = ref(null)
    if (props.list.length) {
      // 查找默认地址
      const defaultAddress = props.list.find((item) => item.isDefault === 0)
      if (defaultAddress) {
        showAddress.value = defaultAddress
      } else {
        showAddress.value = [...props.list[0]]
      }
    }

    // 用于控制对话框显示隐藏双向绑定的
    const dialogVisible = ref(false)

    return { showAddress, dialogVisible }
  }
}
</script>

<style scoped lang="less">
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>
